(function ($) {
  Drupal.behaviors.StoreForm = {
    map: false,
    attach: function (context) {
      let _this = this;
      let lng = Drupal.settings.store.lng;
      let lat = Drupal.settings.store.lat;
      let map = new BMap.Map("container");          // 创建地图实例
      this.map = map;
      if ((lng == null) && (lat == null)) {
        let myCity = new BMap.LocalCity();
        myCity.get(function (result) {
          let lng = result.center.lng;
          let lat = result.center.lat;
          _this.addOverlay(lng, lat);  // 创建点坐标
          _this.setXY({
            lng: lng,
            lat: lat,
          });
        });
      }
      else {
        this.addOverlay(lng, lat);  // 创建点坐标
      }

      //建立一个自动完成的对象
      let ac = new BMap.Autocomplete({
        "input": "edit-search-address",
        "location": map,
      });
      ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
        let str = "";
        let _value = e.fromitem.value;
        let value = "";
        if (e.fromitem.index > -1) {
          value = _value.province + _value.city + _value.district + _value.street + _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
        value = "";
        if (e.toitem.index > -1) {
          _value = e.toitem.value;
          value = _value.province + _value.city + _value.district + _value.street + _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        _this.G("searchResultPanel").innerHTML = str;
      });

      let myValue;
      ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
        let _value = e.item.value;
        myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
        _this.G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
        _this.setPlace(myValue);
      });
    },
    setPlace(myValue) {
      let _this = this;
      let map = this.map;
      map.clearOverlays();    //清除地图上所有覆盖物
      let local = new BMap.LocalSearch(map, { //智能搜索
        onSearchComplete: function () {
          let pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
          _this.map.centerAndZoom(pp, 18);
          let marker = new BMap.Marker(pp);
          _this.map.addOverlay(marker);    //添加标注
          _this.setXY(pp);
          marker.enableDragging();//开启点拖拽
          marker.addEventListener("dragend", function (e) {//添加事件监听，获取拖拽后的点坐标
            //alert("当前位置：" + e.point.lng + ", " + e.point.lat);
            _this.setXY(e.point);
          });
        },
      });
      local.search(myValue);
    },
    convertorPoint(x, y, callback) {
      let convertor = new BMap.Convertor();
      let pointArr = [];
      let ggPoint = new BMap.Point(x, y);
      pointArr.push(ggPoint);
      convertor.translate(pointArr, 3, 5, callback);
    },
    addOverlay(lng, lat) {
      let _this = this;
      this.convertorPoint(lng, lat, function (data) {
        if (data.status === 0) {
          let point = new BMap.Point(data.points[0].lng, data.points[0].lat);  // 创建点坐标
          _this.map.centerAndZoom(point, 12);                 // 初始化地图，设置中心点坐标和地图级别
          let marker = new BMap.Marker(point);  // 创建标注
          _this.map.addOverlay(marker);               // 将标注添加到地图中
          _this.map.enableScrollWheelZoom(true);//开启地图缩放功能
          marker.enableDragging();//开启点拖拽
          marker.addEventListener("dragend", function (e) {//添加事件监听，获取拖拽后的点坐标
            //alert("当前位置：" + e.point.lng + ", " + e.point.lat);
            _this.setXY(e.point);
          });
        }
      });
    },
    setXY: function (point) {
      jQuery("#map input[name=coordinate]").val(5);
      jQuery("#map input[name=lng]").val(point.lng);
      jQuery("#map input[name=lat]").val(point.lat);
    },
    G: function (id) {
      return jQuery(`#${id}`);
    },
  };
})(jQuery);
